{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%load_ext autoreload\n",
    "%autoreload 2\n",
    "\n",
    "import os\n",
    "from datetime import datetime\n",
    "\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "import matplotlib.pyplot as plt\n",
    "from tqdm import tqdm\n",
    "\n",
    "from moment.common import PATHS\n",
    "from moment.utils.ucr_anomaly_archive_subset import ucr_anomaly_archive_subset"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "finetuning_mode = 'linear-probing'\n",
    "experiment_name = \"zero_shot_anomaly_detection\"\n",
    "\n",
    "results_path = os.path.join(PATHS.RESULTS_DIR, experiment_name)\n",
    "print(\"Results path:\", results_path)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "datasets_without_results = []\n",
    "results = []\n",
    "for dataset_name in tqdm(ucr_anomaly_archive_subset, total=len(ucr_anomaly_archive_subset)): \n",
    "    metadata = dataset_name.split('/')[-1].split('_')\n",
    "    data_id, data_name = metadata[0], metadata[3]\n",
    "\n",
    "    if not os.path.exists(os.path.join(results_path, f\"results_{data_id}_{data_name}.csv\")):\n",
    "        datasets_without_results.append(dataset_name)\n",
    "        continue\n",
    "    \n",
    "    best_result_path = os.path.join(results_path, f\"results_{data_id}_{data_name}.csv\")\n",
    "    df = pd.read_csv(best_result_path, index_col=0).T\n",
    "    df['Dataset Name'] = data_name\n",
    "    df['Dataset ID'] = data_id\n",
    "\n",
    "    last_modified_time = int(os.path.getmtime(best_result_path))\n",
    "    last_modified_time = datetime.utcfromtimestamp(last_modified_time).strftime('%d-%m-%Y %H:%M')\n",
    "    df['Timestamp'] = last_modified_time\n",
    "    \n",
    "    results.append(df)\n",
    "\n",
    "results = pd.concat(results, axis=0).reset_index(drop=True)\n",
    "results = results[[\n",
    "    'Dataset ID', 'Dataset Name', 'Adj. Best F1', \n",
    "    'rAUCROC', 'rAUCPR', 'VUSROC', 'VUSPR', 'Timestamp']]\n",
    "\n",
    "results.to_csv(\"../../assets/results/zero_shot/anomaly_detection.csv\", index=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "results.dropna(inplace=True)\n",
    "print(f\"Results: {results.shape}\" )\n",
    "results.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "results[['Adj. Best F1', 'rAUCROC', 'rAUCPR', 'VUSROC', 'VUSPR']].describe()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "labels = ['Adj. Best F1', 'VUSROC']\n",
    "data = results[labels].to_numpy().astype(np.float32)\n",
    "labels = ['Adj. Best $F_1$', 'VUSROC']\n",
    "\n",
    "boxprops = dict(linestyle='-', linewidth=1, color='k')\n",
    "flierprops = dict(marker='o', markersize=12, markeredgecolor='darkgreen')\n",
    "medianprops = dict(linestyle='-', linewidth=2, color='blue')\n",
    "meanpointprops = dict(marker='D', markeredgecolor='black',\n",
    "                      markerfacecolor='firebrick')\n",
    "meanlineprops = dict(linestyle='--', linewidth=2, color='red')\n",
    "\n",
    "_ = plt.boxplot(\n",
    "    x=data, labels=labels, showmeans=True, \n",
    "    meanline=True, bootstrap=10000, notch=True, \n",
    "    boxprops=boxprops, flierprops=flierprops, \n",
    "    medianprops=medianprops, \n",
    "    meanprops=meanlineprops)\n",
    "\n",
    "plt.ylabel(\"Accuracy\", fontsize=16)\n",
    "plt.xlabel(\"Metrics\", fontsize=16)\n",
    "plt.ylim(0.0, None)\n",
    "plt.xticks(fontsize=14)\n",
    "plt.yticks(fontsize=14)\n",
    "plt.grid(color='lightgray', linestyle='--', linewidth=0.5) \n",
    "plt.savefig(\"../../assets/figures/zero_shot/anomaly_detection.png\", dpi=300, bbox_inches='tight')\n",
    "plt.show()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
